package base.db.lib.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import base.constant.ConstantSQL;
import base.db.lib.DBDAO;

@SuppressWarnings("all")
public class ConncenFactory {
	private static final ThreadLocal<Connection> localconnection = new ThreadLocal<Connection>(); //设置数据连接对象
	private static final ThreadLocal<DBDAO> localdao = new ThreadLocal<DBDAO>();                       //设置DBDAO 对象

	/**
	 * 取出 放入ThreadLocal中的ConncenFactory类中 连接对象
	 */
	public static Connection getConnection() {
		Connection connection = localconnection.get();
		if (connection == null) {
			   //0913改写 通过数据源获取链接池
				//ConncenFactory.setConnection(DBConnection.getConnection(ConstantSQL.MSNJOB_SQL_JNDI));
				 //1010改写 通过读取配置信息获取连接
				ConncenFactory.setConnection(DBConnection.getConnectionByConfig());
				connection = localconnection.get();
			//System.out.println("ConncenFactory:" + new Date().toLocaleString()+ "is null!");
		} else {
			//System.out.println("ConncenFactory:idinfo " + connection.toString());
		}
		return connection;
	}
	/**
	 * 取出 放入ThreadLocal中的ConncenFactory类中 设置DBDAO 对象
	 */
	public static DBDAO getDao() {
	    	DBDAO dao=localdao.get();
		if (dao == null) {
			setDao();
			dao=localdao.get();
		} else {
		}
		return dao;
	}
	/**
	 * 根据ConncenFactory 放入ThreadLocal中
	 * @param ConncenFactory
	 */
	public static void setConnection(Connection _connectiont) {
		if (_connectiont != null) {
			try {
				setDao(); //创建对象
				localconnection.set(_connectiont);
				localconnection.get().setAutoCommit(false);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private static void setDao(DBDAO dao) {
		if (dao != null) {	
			localdao.set(dao);
		}
	}
	private static void setDao() {
		if(localdao.get()==null){
			localdao.set(new DBDAO());
		}
	}
	
	/**
	 * 提交事务
	 * @throws SQLException
	 */
	public static void commit() {
		if(localconnection.get()!=null){
			try {
					localconnection.get().commit();
			} catch (Exception e) {
				try {
					localconnection.get().rollback();
				} catch (Exception ex) {
					System.out.println("LOG:[回滚 异常！]");
				}
				System.out.println("LOG:[提交事务 异常！]");
			}
		}
	}
	
	
	/**
	 * 回滚事务
	 * @throws SQLException
	 */
	public static void rollback() {
		if(localconnection.get()!=null){
			try {
					localconnection.get().rollback();
			} catch (Exception e) {
				try {
					localconnection.get().rollback();
				} catch (Exception ex) {
					System.out.println("LOG:[回滚 异常！]");
				}
				System.out.println("LOG:[提交事务 异常！]");
			}
		}
	}
	/**
	 * 提交事务
	 * @throws SQLException
	 */
	public static void commitAndClose() {
		if(localconnection.get()!=null){
			try {
					localconnection.get().commit();
			} catch (Exception e) {
				try {
					localconnection.get().rollback();
				} catch (Exception ex) {
					System.out.println("LOG:[回滚 异常！]");
				}
				colseConnection();
			
				System.out.println("LOG:[提交事务 异常！]");
			}
		}
	}
	/**
	 * 关闭数据库连接
	 * @throws SQLException
	 */
	public static void colseConnection() {
		if(localconnection.get()!=null){
			try {
				localconnection.get().close();
			} catch (Exception e) {
				System.out.println("LOG: [关闭连接异常]");
			} 
		 }
		localconnection.set(null);// 清空当前对象
		localdao.set(null);
	}
	
}
